package DemoReview;
/**
 * 和为 k 的子数组
 */

import java.util.HashMap;
import java.util.Map;

public class LeetCode974 {
    public int subarraysDivByK(int[] nums, int k) {
        int n = nums.length;
        Map<Integer, Integer> hash = new HashMap<>();
        int sum = 0, ret = 0;
        hash.put(0, 1);
        for (int x : nums) {
            sum += x;
            int target = (sum % k + k) % k;
            ret += hash.getOrDefault(target, 0);
            hash.put(target, hash.getOrDefault(target, 0) + 1);
        }
        return ret;
    }
}
